package cn.gok.config;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import sun.security.provider.MD5;

/**
 * @author shikundai
 * @date 2021/3/24
 * @time 11:42
 * @description:
 */
public class ShiroRealm extends AuthorizingRealm {
    @Autowired
    private HashedCredentialsMatcher hashedCredentialsMatcher;
    /**
     * 描述：用户授权
     * @author shikundai
     * @date 2021/3/24
     * @time 11:42
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        SimpleAuthorizationInfo simpleAuthorizationInfo=new SimpleAuthorizationInfo();
        //获得认证的用户
        Object obj=principalCollection.getPrimaryPrincipal();
        System.out.println("授权操作。。。。");
        /*
        * 从数据库查询相应的权限或者角色设置到
        * simpleAuthorizationInfo里面
        * */
        simpleAuthorizationInfo.addRole("teacher");

         return simpleAuthorizationInfo;
    }
    /**
     * 描述：用户认证
     * @author shikundai
     * @date 2021/3/24
     * @time 11:43
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //获得需要认证的数据
        Object obj=authenticationToken.getPrincipal();
        /*
        * 根据这个获得的数据ongoing数据库查询相应的数据
        * "认证数据","密码",getName()
        * 这因为还没接入数据库故直接给模拟数据
        * */
        //注意密码的加密方式需要和配置的一样
        String psw=new Md5Hash("dsk","",1000).toString();


        SimpleAuthenticationInfo info=new SimpleAuthenticationInfo("dsk",psw,getName());

        return info;
    }
}
